import { ajaxAsync } from 'vue-xiaobu-utils'
import OrderListModel from '../models/OrderListModel'
import AjaxForBusOpen from '@/utils/AjaxForBusOpen'
/**
 * 获取定制班线订单列表
 *
 */
async function getcustomOrderList(pageIndex, pageSize, test = false) {
  let data
  let sendObj = {
    GOODS_TYPE: 8, // 7.包车 8.定制班线
    PAGE_CAPACITY: pageSize,
    PAGE_INDEX: pageIndex
  }
  if (test) {
    data = {
      COUNT: 6,
      LIST: [
        {
          ORDER_ID: '213156131',
          ORDER_STATE: 0, // -2:交易关闭，-1:待支付，0:已支付
          PAY_FEE: '60.00',
          PAYMENT: 'ALIPAY',
          OP: [2, 1],
          STORE_ORDER: [
            {
              STORE_NAME: '直达快线售票厅',
              SKU_LIST: [
                {
                  SKU_PROPS_NAME: '次票',
                  SKU_NUM: 4,
                  SKU_PIC:
                    'http://img2.imgtn.bdimg.com/it/u=321406759,120456193&fm=26&gp=0.jpg'
                }
              ]
            }
          ]
        },
        {
          ORDER_ID: '213156131',
          ORDER_STATE: -1, // -2:交易关闭，-1:待支付，0:已支付
          PAY_FEE: '60.00',
          PAYMENT: 'ALIPAY',
          OP: [15],
          STORE_ORDER: [
            {
              STORE_NAME: '直达快线售票厅',
              SKU_LIST: [
                {
                  SKU_PROPS_NAME: '次票',
                  SKU_NUM: 3,
                  SKU_PIC:
                    'http://img1.imgtn.bdimg.com/it/u=938318695,2329256736&fm=11&gp=0.jpg'
                }
              ]
            }
          ]
        },
        {
          ORDER_ID: '213156131',
          ORDER_STATE: -2, // -2:交易关闭，-1:待支付，0:已支付
          PAY_FEE: '60.00',
          PAYMENT: 'ALIPAY',
          OP: [15],
          STORE_ORDER: [
            {
              STORE_NAME: '直达快线售票厅',
              SKU_LIST: [
                {
                  SKU_PROPS_NAME: '次票',
                  SKU_NUM: 2,
                  SKU_PIC:
                    'http://img1.imgtn.bdimg.com/it/u=66050104,2289782529&fm=11&gp=0.jpg'
                }
              ]
            }
          ]
        }
      ]
    }
  } else {
    let res = await ajaxAsync('/order/bos/list', sendObj)
    data = res
  }
  let orderList = []
  if (data.LIST) {
    data.LIST.map((item, index) => {
      // 对后台返回订单名称做处理
      let skuName = item.STORE_ORDER[0].SKU_LIST[0].SKU_PROPS_NAME
        ? item.STORE_ORDER[0].SKU_LIST[0].SKU_PROPS_NAME.split(':')[1]
        : ''
      orderList.push(
        new OrderListModel(
          item.ORDER_ID,
          item.OUT_ORDER_NO,
          item.ORDER_STATE,
          item.PAY_FEE,
          item.OP,
          item.STORE_ORDER[0].STORE_NAME,
          skuName,
          item.STORE_ORDER[0].SKU_LIST[0].SKU_NUM,
          item.STORE_ORDER[0].SKU_LIST[0].SKU_PIC,
          item.PAYMENT
        )
      )
    })
  }
  let body = {
    count: data.COUNT,
    list: orderList
  }
  return body
}

/**
 * 获取包车全局配置信息
 */
async function getGlobals() {
  let postData = {}
  let res
  res = await AjaxForBusOpen('/chartered-bus-api/chartered/globals', postData)
  return res
}

export { getcustomOrderList, getGlobals }
